<template>
  <div class="login">
    <div class="login-form">
      <!-- 表单标题 -->
      <div class="title">
        <h3>后台管理</h3>
      </div>
      <!-- 表单输入框 -->
      <div class="login-input">
        <div class="user">
          <i class="el-icon-user-solid"></i>
          <input
            type="text"
            placeholder="Username"
            v-model.trim="username"
            @blur="vildNotNull"
            @input="vildNotNull"
          />
          <span class="label">用户名不能为空</span>
        </div>
        <div class="pwd">
          <i class="el-icon-lock"></i>
          <input
            type="password"
            ref="pwd"
            placeholder="Password"
            v-model.trim="password"
            @blur="vildNotNull"
            @keyup.enter="login"
            @input="vildNotNull"
          />
          <span class="label">密码不能为空</span>
          <i class="iconfont icon-chakan" ref="iconChakan" @click="showPwd"></i>
          <i
            class="iconfont icon-jinzhichakan"
            ref="jinzhiChakan"
            @click="hidPwd"
          ></i>
        </div>
        <!-- 登录按钮 -->
        <div class="logined" @click="login">Login</div>
      </div>
    </div>
    <div class="cirle"></div>
  </div>
</template>

<script>
// 从 vuex 中按需导入辅助函数
import { mapActions, mapState } from 'vuex'
// 导入js-cookie
import Cookie from 'js-cookie'
export default {
  name: 'Login',
  data() {
    return {
      // 用户名
      username: 'admin',
      // 密码
      password: 'admin'
    }
  },
  computed: {
    // 将user仓库的共享数据映射为当前组件的计算属性
    ...mapState('user', ['token'])
  },
  methods: {
    // 将 user 仓库的一部方法 映射为当前组件的方法
    ...mapActions('user', ['loginRequest']),
    // 点击眼睛隐藏密码
    showPwd(e) {
      // 隐藏眼睛
      e.target.style.display = 'none'
      // 显示划眼
      this.$refs.jinzhiChakan.style.display = 'block'
      // 隐藏密码
      this.$refs.pwd.type = 'password'
    },
    // 点击划眼显示密码
    hidPwd(e) {
      // 隐藏划眼
      e.target.style.display = 'none'
      // 显示眼睛
      this.$refs.iconChakan.style.display = 'block'
      // 隐藏密码
      this.$refs.pwd.type = 'text'
    },
    // 输入框失去光标判断内容是否为空
    vildNotNull(e) {
      if (!e.target.value) {
        e.target.nextElementSibling.style.visibility = 'visible'
        e.target.style.borderColor = '#EF629F'
      } else {
        e.target.nextElementSibling.style.visibility = 'hidden'
        e.target.style.borderColor = '#93acce'
      }
    },
    // 登录
    async login() {
      // 判断密码长度是否大于等于5位
      // 验证用户名的正则
      const self = this
      const regUser = /^[\S]{5,16}$/
      if (!this.username && !this.password) return this.$message.error('请填写用户名和密码')
      if (!regUser.test(this.username)) return this.$message.error('用户名不能小于5位数')
      if (!regUser.test(this.password)) return this.$message.error('密码不能小于5位数')
      try {
        const params = {
          username: this.username,
          password: this.password
        }
        const success = await this.loginRequest(params)
        this.$message.success(success)
        Cookie.set('token', this.token, { expires: 7 })
        self.$router.push({
          name: 'home'
        })
      } catch (err) {
        this.$message.error(err.message)
      }
    }
  }
}
</script>

<style lang="less" scoped>
.login {
  position: relative;
  display: flex;
  overflow: hidden;
  align-items: center;
  height: 100%;
  background-color: #93acce;
  .cirle {
    position: absolute;
    bottom: -395px;
    left: -402px;
    width: 800px;
    height: 800px;
    background-color: #ffffff;
    border-radius: 50%;
    box-shadow: 0 0 8px 0 rgba(0, 0, 0, .3);
    z-index: 99;
  }
  &::before {
    position: absolute;
    top: -82px;
    left: -900px;
    content: '';
    width: 2000px;
    height: 2000px;
    background-color: #ffffff;
    border-radius: 50%;
    box-shadow: 0 0 10px 0 rgba(0, 0, 0, .5);
  }
  &::after {
    position: absolute;
    top: 175px;
    left: -748px;
    content: '';
    width: 1500px;
    height: 1500px;
    background-color:#93acce;
    border-radius: 50%;
    box-shadow: 0 0 10px 0 rgba(0, 0, 0, .5);
  }
  .login-form {
    position: absolute;
    left: 60%;
    width: 560px;
    height: 400px;
    background-color: rgba(2505, 255, 255, 1);
    border-radius: 20px;
    box-shadow: 2px 2px 6px rgba(0, 0, 0, .5);
    .title {
      margin-top: 15px;
      h3 {
        color: #93acce;
        font-size: 28px;
        text-align: center;
        padding: 20px 0;
      }
    }
    .login-input {
      display: flex;
      flex-direction: column;
      align-items: center;
      .user,
      .pwd {
        display: flex;
        flex-direction: column;
        align-items: center;
        width: 100%;
        input {
          width: 80%;
          height: 50px;
          margin-top: 30px;
          padding: 0 40px;
          background-color: #ffffff;
          border-radius: 5px;
          border: 1px solid #93acce;
          color: #aac6ee;
          font-size: 14px;
          outline: none;
          &::placeholder {
            color: #CFDEF3;
            font-size: 14px;
          }
        }
      }
      .label {
        visibility: hidden;
        position: absolute;
        bottom: -17px;
        left: 90px;
        font-size: 12px;
        color: #EF629F;
      }
      .user {
        position: relative;
        .el-icon-user-solid {
          position: absolute;
          top: 50%;
          left: 70px;
          transform: translateY(6px);
          color: #93acce;
        }
      }
      .pwd {
        position: relative;
        .el-icon-lock {
          position: absolute;
          top: 50%;
          left: 70px;
          transform: translateY(7px);
          color: #93acce;
        }
        .icon-chakan {
          display: none;
        }
        .icon-chakan,
        .icon-jinzhichakan {
          position: absolute;
          top: 50%;
          right: 70px;
          transform: translateY(7px);
          color: #93acce;
          cursor: pointer;
        }
      }
    }
    .logined {
      margin-top: 30px;
      width: 80%;
      height: 50px;
      line-height: 50px;
      text-align: center;
      color: #ffffff;
      background-color: #93acce;
      border-radius: 5px;
      cursor: pointer;
      transition: all 0.3s;
      &:hover {
        box-shadow: 0 0 10px 0 #93acce;
      }
    }
  }
}
</style>
